package common.zsxmlv.utils;


import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: ZSXM
 * @Description:
 * @Company: QIDI
 * @Date: Create by 2018/10/12 10:29
 * @Version: 1.0
 * @Modify by:
 */
public class ReadFileTime {

    public static String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";

//    public static String FILEPATH = "E:\\develop\\java\\QIDI\\project_SVN\\99.trainProject\\05.开发阶段";
//public static String FILEPATH = "E:\\develop\\java\\fupaibao\\apache-tomcat-8.5.15\\webapps\\ROOT\\WEB-INF\\classes\\com";
    public static String FILEPATH = "E:\\develop\\java\\fupaibao\\apache-tomcat-8.5.15\\webapps\\ROOT\\templets\\1";

    public static List<List<String>> list = new ArrayList<List<String>>();

    public static void main(String[] args) throws FileNotFoundException, ParseException {
//        String s = "哈哈哈1";
//        String substring = s.substring(s.length() - 1);
//        System.out.println(substring);
        getFiles(FILEPATH);
//        OutputStream out = new FileOutputStream("d:/aa.xlsx");
//        try {
////            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
////            //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
////            Sheet sheet1 = new Sheet(1, 0,ChangeFile.class);
////            writer.write0(list, sheet1);
////            writer.finish();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                out.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }

    public static void readFileDirectory(String filePath) {
        File file = new File(filePath);
        String format = new SimpleDateFormat(FORMAT_TIME).format(new Date(file.lastModified()));
        // System.out.println(format);
    }

    public static void getFiles(String filePath) throws ParseException {
        File file = new File(filePath);
//        System.out.println("[文件]\t" + file.getName() + "\t\t[路径]\t" + file.getPath());
        String time = new SimpleDateFormat(FORMAT_TIME).format(new Date(file.lastModified()));
        Date parse = new SimpleDateFormat("yyyy-MM-dd HH").parse("2018-11-27 01");
        long lastModified = file.lastModified();
        if (file.getPath().indexOf("InfoModule")>-1){
            System.out.println("\t" + file.getName() + "\t\t\t\t" + time + "\t\t\t\t" + file.getPath());
        }

        if (file.isFile() && !excludeFile(file) && !"2018-07-20 09".equals(time.substring(0,13)) && lastModified>parse.getTime()) {
//            System.out.println("[文件]\t" + file.getName() + "\t\t\t\t[创建时间]\t" + time + "\t\t\t\t\t\t[全路径]\t" + file.getPath());
            System.out.println("\t" + file.getName() + "\t\t\t\t" + time + "\t\t\t\t" + file.getPath());
//            ChangeFile changeFile = new ChangeFile(file.getName(), file.getPath(), file.lastModified() + "");
//            list.add(changeFile);
            List<String> list1 = new ArrayList<String>();
            list1.add(file.getName());
            list1.add(file.getPath());
            list1.add(file.lastModified()+"");
            list.add(list1);
        }

        if (file.isDirectory()) {
            if (file.getName().equals("target")) {
                return;
            }
            File[] files = file.listFiles();
            for (File f : files) {
                getFiles(f.getPath());
            }
        }
    }


    /**
     * 排除指定文件
     * @param file
     * @return
     */
    public static boolean excludeFile(File file) {
        String name = file.getName();
        String substring = file.getName().substring(name.lastIndexOf(".")+1);
        // ,"class","xml","css"
        String[] fileSuffix = {"png","gif","jpg","jar","php","properties","idea","zip","meta","iml","bk"};
        for (String suf : fileSuffix) {
            if (substring.equals(suf)) {
                return true;
            }
        }
        return false;
    }

}
